package com.lovo.web.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class RabbitConfig {

    @Autowired
    CachingConnectionFactory connectionFactory;
    //rabbitmq模板
    @Bean(value = "rabbitTemplate")
    public RabbitTemplate rabbitTemplate(){
        return new RabbitTemplate(connectionFactory);
    }
    //创建队列
    @Bean
    public Queue queueOne(){
        return new Queue("queueOne");
    }

    //创建交换机
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange("directExchange");
    }
    //交换机与队列绑定
    @Bean
    public Binding directExchangeToQueueOne(Queue queueOne,DirectExchange directExchange){
        return BindingBuilder.bind(queueOne).to(directExchange).with("replenishUserInfo.key");
    }
    //创建发送给审核系统和后台系统的订单交换机
    @Bean
    public DirectExchange orderExchange(){
        return new DirectExchange("orderExchange");
    }
    //创建发送给审核系统的充值记录交换机
    @Bean
    public DirectExchange depositHistoryTwoExchange(){
        return new DirectExchange("depositHistoryTwoExchange");
    }
    //创建发送给审核系统和后台系统的退款详情交换机
    @Bean
    public DirectExchange refundTwoExchange(){
        return new DirectExchange("refundTwoExchange");
    }
    //创建发送给审核系统的订单队列
    @Bean
    public Queue orderToCscQueue(){
        return new Queue("orderToCscQueue");
    }
    //创建发送给后台系统的订单队列
    @Bean
    public Queue orderToSscSysQueue(){
        return new Queue("orderToSscSysQueue");
    }
    //创建发送给审核系统的退款详情队列
    @Bean
    public Queue refundToCscQueue(){
        return new Queue("refundToCscQueue");
    }
    //创建发送给后台系统的退款详情队列
    @Bean
    public Queue refundToSysQueue(){
        return new Queue("refundToSysQueue");
    }
    //创建发送给审核系统的充值记录队列
    @Bean
    public Queue queueDepositHistoryTwo(){
        return new Queue("queueDepositHistoryTwo");
    }
    //绑定审核系统的订单队列到订单交换机
    @Bean
    public Binding orderToCscQueueToOrderExchange(Queue orderToCscQueue,DirectExchange orderExchange){
        return BindingBuilder.bind(orderToCscQueue).to(orderExchange).with("cscOrder.key");
    }
    //绑定后台系统的订单队列到订单交换机
    @Bean
    public Binding orderToSscSysQueueToOrderExchange(Queue orderToSscSysQueue,DirectExchange orderExchange){
        return BindingBuilder.bind(orderToSscSysQueue).to(orderExchange).with("sscSysOrder.key");
    }
    //绑定审核系统的充值记录队列到充值记录交换机
    @Bean
    public Binding queueDepositHistoryTwoToDepositHistoryTwoExchange(Queue queueDepositHistoryTwo,DirectExchange depositHistoryTwoExchange){
        return BindingBuilder.bind(queueDepositHistoryTwo).to(depositHistoryTwoExchange).with("depositHistoryTwo.key");
    }
    //绑定审核系统的退款详情队列到订单交换机
    @Bean
    public Binding refundToCscQueueToRefundTwoExchange(Queue refundToCscQueue,DirectExchange refundTwoExchange){
        return BindingBuilder.bind(refundToCscQueue).to(refundTwoExchange).with("cscTwoRefund.key");
    }
    //绑定后台系统的退款详情队列到订单交换机
    @Bean
    public Binding refundToSysQueueToRefundTwoExchange(Queue refundToSysQueue,DirectExchange refundTwoExchange){
        return BindingBuilder.bind(refundToSysQueue).to(refundTwoExchange).with("sysTwoRefund.key");
    }

}
